<?php

namespace app\common\service\UserService;

use app\model\BbcModel\BusinessLevel;
use app\model\BbcModel\ChannelBusiness;
use app\model\BbcModel\Order;
use app\model\BbcModel\ServiceProvider;
use app\model\BbcModel\Store;
use app\Normal;
use app\model\BbcModel\StoreMember;


/**
 * 会员业务
 * @author Administrator
 */
class MemberService
{


    /**
     * 组装查询条件
     * @param array $param
     * @return array
     */
    public function makeWhere(array $param)
    {
        $where=[];
        $where[]=['s.df','=',0];
        //服务商id
        if(!empty($param['sp_id'])){
            $where[]=['s.sp_id','=',$param['sp_id']];
        }
        //大B id
        if(!empty($param['cid'])){
            $where[]=['s.cid','=',$param['cid']];
        }
        //渠道商id
        if(!empty($param['cb_id'])){
            $where[]=['s.cb_id','=',$param['cb_id']];
        }
        //店铺名字
        if(!empty($param['mn_sid'])){
            $where[]=['s.mn_sid','=',$param['mn_sid']];
        }
        //注册手机号
        if(!empty($param['mobile'])){
            $where[]=['m.mobile','like','%'.$param['mobile'].'%'];
        }
        //昵称
        if(!empty($param['nick_name'])){
            $where[]=['s.nick_name','like','%'.$param['nick_name'].'%'];
        }
        //类型
        if(!empty($param['type'])){
            if($param['type']==1){
                $where[]=['s.vip_expired','>',time()];
            }else{
                $where[]=['s.vip_expired','<',time()];
            }
        }
        //服务商列表
        $service=ServiceProvider::instance()->lists(['_fields_'=>'id,name']);
        $service=array_column($service,'name','id');
        //渠道商列表
        $business=ChannelBusiness::instance()->lists(['_fields_'=>'id,name']);
        $business=array_column($business,'name','id');
        //店铺列表
        $store=Store::instance()->lists(['_fields_'=>'id,store_name']);
        $store=array_column($store,'store_name','id');
        return [
            'where'=>$where,
            'service'=>$service,
            'business'=>$business,
            'store'=>$store,
        ];
    }


    /**
     * 会员列表
     * @param array $param
     * @return array
     */
    public function memberList(array $param)
    {
        $check=$this->makeWhere($param);
        $page=(isset($param['page'])&&$param['page']>0)?$param['page']:1;
        $limit=(isset($param['limit'])&&$param['limit']>0)?$param['limit']:10;
        $p=($page-1)*$limit;
        $where=$check['where'];
        $service=$check['service'];
        $business=$check['business'];
        $store=$check['store'];
        $fields="m.mid,m.mobile,s.id,s.sp_id,s.cb_id,s.mn_sid,s.cd,s.pay_time,s.vip_expired,s.nick_name";
        $list=StoreMember::instance()->alias('s')
            ->field($fields)
            ->leftjoin('member m','m.mid=s.mid')
            ->where($where)
            ->order('s.id desc')
            ->limit($p, $limit)
            ->select()
            ->toArray();
        $time=time();
        foreach ($list as $k=>$v){
            $list[$k]['store_name']=$store[$v['mn_sid']];
            $list[$k]['channel_name']=$business[$v['cb_id']];
            $list[$k]['service_name']=$service[$v['sp_id']];
            $list[$k]['cd']=date('Y-m-d H:i:s',$v['cd']);
            $list[$k]['pay_time']=$v['pay_time']>0?date('Y-m-d H:i:s',$v['pay_time']):'';
            $list[$k]['vip_expired']=$v['vip_expired']>0?date('Y-m-d H:i:s',$v['vip_expired']):'';
            $list[$k]['type']=$v['vip_expired']>$time?'会员':'普通用户';
            $orderList=Order::instance()->lists(['mid'=>$v['mid'],'mn_sid'=>$v['mn_sid'],'pay_status'=>1,'_fields_'=>'price_order']);
            $list[$k]['order_num']=count($orderList);
            $list[$k]['order_price']=array_sum(array_column($orderList,'price_order'));
        }
        $num=StoreMember::instance()->alias('s')
            ->leftjoin('member m','m.mid=s.mid')
            ->where($where)
            ->count();
        $data['total']=$num;
        $data['list']=$list;
        return Normal::returnCode(Normal::SUC,"请求成功",$data);
    }
}
